﻿
layui.define(['jquery','index', 'upload','element'], function (exports) {
    var $ = layui.jquery,
        admin = layui.admin,
        upload = layui.upload,
    element = layui.element;
        
	// 弹窗
	var active = {
		iframe: function (url, title, childContent, minSizeArr,maxSizeArr) {
			layer.open({
				title: title,
				type: 2,
				content: url,
				shadeClose: true,
				area: admin.screen() < 2 ? minSizeArr : maxSizeArr,
				maxmin: true,
				success: function (layero, index) {
					var content = childContent || "";
                    var iframe = window['layui-layer-iframe' + index]; // 不同的弹窗索引
                    iframe.child(content); // 父页面与子页面（弹窗）通信
				}
			});
		},
        uploadfile: function (downloadObj) {
            console.log(downloadObj);
            var totalPage = "";
            var page = 1;
            var status = 1;
            //还要添加上传正则检查格式
            upload.render({
                elem:   downloadObj.elem   || "", //绑定元素
                url:    downloadObj.url    || "", //上传接口
                accept: downloadObj.file   || "",
                auto:   downloadObj.auto   || false,
                acceptMime: downloadObj.acceptMime || "",//'.xlsx,.docx,.zip,.rar',//允许上传的文件类型 .docx,.zip,.rar
                choose: function (obj) {
                    
                    //$("#upload_progress").removeClass("layui-hide");
                    //element.progress('uploadProgress', '0%');
                    //$("button[name='submit']").addClass("layui-btn layui-btn-disabled");
                    //$("button[name='submit']").attr("disabled", "disabled");
                    var data = this.data;
                    var files = obj.pushFile();
                    var LENGTH = 5120 * 1024; //每片文件大小
                    obj.preview(function (index, file, result) {

                        totalSize = file.size;
                        totalPage = Math.ceil(totalSize / LENGTH);

                        var fileName = file.name;
                        var fileExt = fileName.substr(fileName.lastIndexOf('.') + 1);//上传文件类型

                        fileName = fileName.substr(0, fileName.lastIndexOf('.'));//文件名称*/
                        console.log(fileName);

                        var progresstimer = setInterval(function () {
                            var i = 1;
                            totalPage = parseInt(totalPage);
                            page = parseInt("1");
                            if (parseInt(totalPage) == parseInt(page) && (parseInt(status) == 2 || parseInt(status) == -1)) {
                                clearInterval(progresstimer);
                            } else {
                                if (status == 1) {
                                    status = 0;
                                    data.fileName = fileName;
                                    data.page = page;
                                    data.totalPage = totalPage;
                                    data.fileExt = fileExt;
                                    obj.upload(index, file.slice((page - 1) * length, page * length));//)
                                }
                            }
                        }, 100);
                    });
                },
                done: function (res) {
                   
                    console.log(res)
                    if (res.code == 1) { //分片上传
                        page = parseInt(page);
                        totalPage = parseInt(totalPage);
                        element.progress('uploadProgress', Math.ceil(page * 100 / totalPage) + '%');
                        page = page + 1;
                        status = 1;
                    } else if (res.code == 2) { //上传完成
                        element.progress('uploadProgress', '100%');
                        status = 2;
                        layer.msg('上传成功', {
                            offset: '15px',
                            icon: 1,
                            time: 1000
                        }, function () {
                            console.log(status)
                        });
                    }
                    else if (res.code == 3) { //上传完成
                        status = -1;
                        element.progress('uploadProgress', '0%');
                        layer.msg('上传失败，请注意上传文件格式', {
                            offset: '15px',
                            icon: 2,
                            time: 1000
                        }, function () {
                            console.log(status)
                        });
                    } else { //上传错误
                        status = -1;
                        //$('#status').val('-1');
                        element.progress('uploadProgress', '0%');
                        // console.log(!typeof (res.downUrl) == "undefined");
                        layer.msg('上传错误', {
                            offset: '15px',
                            icon: 2,
                            time: 1000
                        }, function () {
                            console.log(status)
                        });
                    }
                },
                error: function (res) {
                    console.log(res)
                }
            });
		}
	}
	//输出接口
	exports('public', active);
});